<template>
	<view class="newsPage">
		<!-- <view class="tabAndFilter" v-if="isLoad">
			<view class="tabContent">
				<view @click="doFilter(item)" v-for="(item,index) in tabs" :key="index" class="tabItem"
					:class="activeTab===item.id?'isActive':''">{{item.classificationName}}</view>
			</view>
		</view> -->
		<view class="newsDetial">
			<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png" v-if="!list.length && !isLoading"
				text="没有更多了"></u-empty>
			<view class="solutionList" v-for="(item,index) in list" :key="index" @click="goDetail(item)">
				<view class="solutionInfo">
					<view class="solutionTitle">{{item.name}}</view>
					<view class="detailInfo">
						<view>{{getReleaseTime(item.releaseTime)}}</view>
						<view>{{item.readingVolume}}阅读</view>
					</view>
				</view>
				<view class="solutionImage">
					<u--image :src="item.images?(api+'/sys/common/static/'+item.images):''" shape="square"
						mode="aspectFill" width="208rpx" height="150rpx" radius="8rpx">
					</u--image>
				</view>
			</view>
			<u-loadmore v-if="list.length" :status="isFinish?'nomore':'loading'" loading-text="正在加载"
				loadmore-text="加载更多" nomore-text="没有更多了" color="#d0d0d0" />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				api: this.$api.baseUrl,
				list: [],
				pageNo: 1,
				pageSize: 10,
				total: 0,
				isFinish: false,
				isLoading: false,
				activeTab: "",
				tabs: [],
				isLoad: false
			}
		},
		computed: {
			getReleaseTime(date) {
				return function(date) {
					if (date) {
						if (typeof date === 'number') {
							return new Date(date).format("YYYY-MM-dd hh:mm")
						} else {
							return new Date(date.replace(/\-/g, "/")).format("YYYY-MM-dd hh:mm")
						}
					} else {
						return ""
					}
				}
			},
		},
		onReachBottom() {
			if (!this.isLoading && !this.isFinish) {
				this.pageNo++;
				this.initData();
			}
		},
		onPullDownRefresh() {
			this.isFinish = false;
			if (!this.isLoading) {
				this.pageNo = 1;
				this.list = [];
				this.initData(true);
			}
		},
		onLoad() {
			uni.getStorage({
				key: "userInfo",
				success: (userInfo) => {
					this.userId = userInfo.data.id;
					this.$request.get(this.$api.getNewsColumns, {
						pageNo: 1,
						pageSize: 100
					}).then(res => {
						this.tabs = res.result.records;
						this.tabs.unshift({
							classificationName: "全部",
							id: ""
						})
						this.activeTab = this.tabs[0].id;
						this.initData();
						uni.hideLoading()
					})
				}
			})
		},
		methods: {
			onReachBottom() {
				if (!this.isLoading && !this.isFinish) {
					this.pageNo++;
					this.initData();
				}
			},
			onPullDownRefresh() {
				this.doSearch();
			},
			initData(isRefresh) {
				this.isLoading = true;
				Promise.all([
					this.$request.get(this.$api.getNewsList, {
						type: '2',
						pageNo: this.pageNo,
						pageSize: this.pageSize,
						classificationId: this.activeTab
					}),
				]).then(res => {
					
					console.info(res[0].result.records)
					if (isRefresh) {
						uni.stopPullDownRefresh()
					}
					this.isLoading = false;
					this.list = this.list.concat(res[0].result.records)
					this.total = res[0].result.total;
					if (this.pageNo * this.pageSize < this.total) {
						this.isFinish = false
					} else {
						this.isFinish = true
					}
					this.isLoad = true;
					uni.hideLoading()
				})
			},
			doSearch() {
				this.isFinish = false;
				if (!this.isLoading) {
					this.pageNo = 1;
					this.list = [];
					this.initData(true);
				}
			},
			doFilter(item) {
				if (this.activeTab === item.id) return;
				this.activeTab = item.id;
				this.doSearch()
			},
			goDetail(item) {
				uni.navigateTo({
					url: "../solutionDetial/solutionDetial?id=" + item.id,
				})
			}
		}
	}
</script>

<style lang="scss">
	.newsPage {
		.tabAndFilter {
			display: flex;
			justify-content: flex-start;

			.tabContent {
				display: flex;
				flex-wrap: nowrap;
				justify-content: flex-start;
				padding: 0 40rpx 20rpx;
				width: calc(100vw - 120rpx);
				overflow: auto;

				.tabItem {
					background: #F4F4F4;
					border-radius: 4px 4px 4px 4px;
					color: #333333FF;
					font-size: 26rpx;
					height: 62rpx;
					padding: 0 20rpx;
					line-height: 62rpx;
					margin-right: 20rpx;
					flex-shrink: 0;
				}

				.isActive {
					color: #004DA1FF;
					background-color: #E5EDFFFF;
				}
			}

		}
	}

	.newsDetial {
		padding: 0 40rpx 40rpx 40rpx;

		.solutionList {
			background: #FFFFFF;
			box-shadow: inset 0px 0px 0px 1rpx rgba(230, 230, 230, .5);
			border-radius: 20rpx;
			margin: 20rpx 0;
			padding: 30rpx;
			display: flex;
			flex-wrap: nowrap;

			.solutionInfo {
				display: flex;
				justify-content: space-between;
				flex-direction: column;
				width: calc(100% - 208rpx);
				padding-right: 20rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: bold;
				color: #333333;
				font-size: 28rpx;

				.solutionTitle {
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
				}

				.detailInfo {
					display: flex;
					flex-wrap: nowrap;
					justify-content: space-between;
					flex-shrink: 0;
					padding-right: 20rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #999999;
					font-size: 26rpx;
				}
			}
		}
	}
</style>
